package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCourse
//@description: 创建Course记录
//@param: course model.Course
//@return: err error

func CreateCourse(course model.Course) (err error) {
	err = global.GVA_DB.Create(&course).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCourse
//@description: 删除Course记录
//@param: course model.Course
//@return: err error

func DeleteCourse(course model.Course) (err error) {
	err = global.GVA_DB.Delete(&course).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCourseByIds
//@description: 批量删除Course记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCourseByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Course{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCourse
//@description: 更新Course记录
//@param: course *model.Course
//@return: err error

func UpdateCourse(course model.Course) (err error) {
	err = global.GVA_DB.Save(&course).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCourse
//@description: 根据id获取Course记录
//@param: id uint
//@return: err error, course model.Course

func GetCourse(id uint) (err error, course model.Course) {
	err = global.GVA_DB.Where("id = ?", id).First(&course).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCourseInfoList
//@description: 分页获取Course记录
//@param: info request.CourseSearch
//@return: err error, list interface{}, total int64

func GetCourseInfoList(info request.CourseSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Course{})
    var courses []model.Course
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&courses).Error
	return err, courses, total
}